package com.easonji.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.easonji.admin.form.AppForm;
import com.easonji.admin.mapper.PermissionMapper;
import com.easonji.admin.model.UserApp;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.easonji.admin.mapper.AppMapper;
import com.easonji.admin.mapper.UserAppMapper;
import com.easonji.admin.model.App;
import com.easonji.admin.model.Permission;
import com.easonji.admin.query.AppQuery;
import com.easonji.admin.service.AppService;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.constant.cache.CacheConsts;
import com.easonji.common.constant.cache.CacheKeyConsts;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;
import com.easonji.core.annotion.CacheRemove;
import com.easonji.core.exception.BizException;

/**
 * @ClassName: AppService
 * @Description: 应用服务实现类
 * @Author: LiuJun
 * @date: 2018/07/30 14:07:42
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Service
public class AppServiceImpl extends AbstractBaseService<App> implements AppService {

    @Resource
    private AppMapper appMapper;

    @Resource
    private UserAppMapper userAppMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public Object queryAppList(AppQuery appQuery) {
        // 查询条件
        Wrapper<App> wrapper = new EntityWrapper<>();
        String condition = appQuery.getCondition();
        if (StringUtils.isNotEmpty(condition)) {
            wrapper.like("code", condition).or().like("name", condition);
        }
        // 排序
        List<String> columns = new ArrayList<>();
        columns.add("num");
        wrapper.orderAsc(columns);
        List<App> apps = appMapper.selectList(wrapper);
        return warp(apps);
    }

    @Override
    public Object queryApp(Integer appId) {
        App app = appMapper.selectById(appId);
        return warp(app);
    }

    @Override
    @CacheRemove(value = CacheConsts.CONSTS, key = { CacheKeyConsts.APP })
    public Integer saveApp(AppForm appForm) {
        App app = new App();
        BeanUtils.copyProperties(appForm, app);
        return appMapper.insert(app);
    }

    @Override
    @CacheRemove(value = CacheConsts.CONSTS, key = { CacheKeyConsts.APP })
    public Integer updateApp(AppForm appForm) {
        App app = new App();
        BeanUtils.copyProperties(appForm, app);
        return appMapper.updateById(app);
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    @CacheRemove(value = CacheConsts.CONSTS, key = { CacheKeyConsts.APP })
    public Boolean removeApp(Integer appId) {
        // 删除应用对应的用户
        Wrapper<UserApp> wrapper = new EntityWrapper<>();
        wrapper.eq("app_id", appId);
        userAppMapper.delete(wrapper);
        // 删除应用对象的权限
        Wrapper<Permission> permissionWrapper = new EntityWrapper<>();
        permissionWrapper.eq("app_id", appId);
        permissionMapper.delete(permissionWrapper);
        // 删除应用
        App app = new App();
        app.setId(appId);
        return app.deleteById();
    }

    @Override
    protected JSONObject warp2Json(App app) {
        JSONObject result = JsonUtils.parseObject(app);
        return result;
    }
}
